﻿Imports System.IO

Public Class FDSiswa

    'member variables
    Private _idSiswa As Integer
    Private _cSiswa As CSiswa
    Private _aSiswa As DataTable

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Text = My.Settings.NamaAplikasi & " [Detil Siswa]"
    End Sub

#Region "SettinganAwal"
    '-----------------------------------------------------------------------------------------------
    Private IsFormBeingDragged As Boolean = False
    Private MouseDownX As Integer
    Private MouseDownY As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseDown, LabelJudul.MouseDown

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = True
            MouseDownX = e.X
            MouseDownY = e.Y
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseUp, LabelJudul.MouseUp

        If e.Button = MouseButtons.Left Then
            IsFormBeingDragged = False
        End If
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles MyBase.MouseMove, LabelJudul.MouseMove

        If IsFormBeingDragged Then
            Dim temp As Point = New Point()

            temp.X = Me.Location.X + (e.X - MouseDownX)
            temp.Y = Me.Location.Y + (e.Y - MouseDownY)
            Me.Location = temp
            temp = Nothing
        End If
    End Sub


    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim p As New Pen(Color.FromArgb(213, 251, 185), 2)
        'set text box border
        PaintTextBox(Me, e.Graphics, p)

        p.Dispose()
    End Sub

    Private Sub PaintTextBox(ByVal cont As Control, ByVal g As Graphics, ByVal p As Pen)
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is TextBox Then
                g.DrawRectangle(p, New Rectangle(ctl.Location + New Size(1, 1), ctl.Size - New Size(2, 2)))
            ElseIf TypeOf ctl Is Panel Then
                PaintTextBox(ctl, g, p)
            ElseIf TypeOf ctl Is GroupBox Then
                PaintTextBox(ctl, g, p)
            End If
        Next
    End Sub

    Private Sub SetRegionTextBox(ByVal cont As Control)
        For Each ctl As Control In cont.Controls
            If (TypeOf ctl Is TextBox) Or (TypeOf ctl Is PictureBox) Or (TypeOf ctl Is ComboBox) Then
                If ctl.Region Is Nothing Then
                    ctl.Region = New Region(New Rectangle(2, 2, ctl.Width - 4, ctl.Height - 4))
                Else
                    ctl.Region = Nothing
                End If
            ElseIf TypeOf ctl Is Panel Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is GroupBox Then
                SetRegionTextBox(ctl)
            ElseIf TypeOf ctl Is TabControl Then
                For Each tab As Control In CType(ctl, TabControl).TabPages
                    SetRegionTextBox(tab)
                Next
            End If
        Next
    End Sub


    '-----------------------------------------------------------------------------------------------
#End Region




    ''' <summary>
    ''' menampilkan detil siswa
    ''' </summary>
    ''' <param name="idSiswa"></param>
    ''' <remarks></remarks>
    Public Sub New(ByVal idSiswa As Integer)
        Me.New()
        Me._idSiswa = idSiswa
    End Sub

    Private Sub FDSiswa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'set text box border
        SetRegionTextBox(Me)

        'menampilkan data siswa
        If _idSiswa > 0 Then
            _cSiswa = New CSiswa
            _aSiswa = _cSiswa.getById(Me._idSiswa)

            'tampilkan ke control
            lblNama.Text = _aSiswa(0).Item("nama").ToString
            lblNISN.Text = _aSiswa(0).Item("tmpt_lhr").ToString

            tbNama.Text = _aSiswa(0).Item("nama").ToString
            TBNisn.Text = _aSiswa(0).Item("nisn").ToString
            tbTempatLahir.Text = _aSiswa(0).Item("tmpt_lhr").ToString
            tbTglLahir.Text = CDate(_aSiswa(0).Item("tgl_lhr")).Day
            tbBlnLahir.Text = CDate(_aSiswa(0).Item("tgl_lhr")).Month
            tbThnLahir.Text = CDate(_aSiswa(0).Item("tgl_lhr")).Year
            cbJK.SelectedItem = _aSiswa(0).Item("jk").ToString
            tbAlamat.Text = _aSiswa(0).Item("almt").ToString
            tbAnakke.Text = _aSiswa(0).Item("ank_ke").ToString
            tbJmlSaudara.Text = _aSiswa(0).Item("jml_sdr").ToString
            tbFoto.Text = _aSiswa(0).Item("foto").ToString

            tbNamaAyah.Text = _aSiswa(0).Item("nm_ayh").ToString
            tbTelpAyah.Text = _aSiswa(0).Item("telp_ayh").ToString
            'tbPekerjaanAyah .Text = _aSiswa(0).Item("tmpt_lhr").toString
            tbNamaIbu.Text = _aSiswa(0).Item("nm_ibu").ToString
            tbTelpIbu.Text = _aSiswa(0).Item("telp_ibu").ToString
            'tbPekerjaanIbu .Text=_aSiswa(0).Item("tmpt_lhr").toString


            If _aSiswa(0).Item("foto").ToString = "" Then
                PictureBox1.Image = My.Resources.faceicon
            Else
                Try
                    PictureBox1.Image = Image.FromFile(My.Settings.DirektoriFoto & "\" & _aSiswa(0).Item("foto").ToString)
                Catch ex As FileNotFoundException
                    PictureBox1.Image = My.Resources.faceicon
                End Try
            End If

        End If
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        Dim _fmcfoto As New FMcFoto(PictureBox1.Image)
        _fmcfoto.ShowDialog()
    End Sub

    Private Sub btnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKeluar.Click
        Me.Close()
    End Sub

    Private Sub btnMinimize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinimize.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub

    Private Sub btnMaximize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMaximize.Click
        WindowState = FormWindowState.Maximized
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        btnKeluar.PerformClick()
    End Sub
End Class